import { defineStore } from 'pinia'

type TRole = {
  id: string | number
  description: string
  enterpriseId: number
  role: number
  roleType: string
  leaderTypeId: number
  departmentId: string
  platform: string
  type: number
}
type TUserLogin = {
  account: string
  id: number
  mobile: string
  type: number
  username: string
  platform: number
}
interface IOUserInfo {
  openUserToken: string
  username: string
  realName: string
  id: string | number
  selectedRoleId: string | number
  roleName: string
  enterpriseId: string | number
  departmentId: string
  departmentName: string
  orgId: string
  roles?: Array<TRole>
  userLogin?: TUserLogin
}
interface IOUser {
  userInfo: IOUserInfo
}
export const useUserStore = defineStore('user', {
  state: (): IOUser => {
    return {
      userInfo: {
        openUserToken: '',
        username: '',
        realName: '',
        id: '',
        selectedRoleId: '',
        roleName: '',
        enterpriseId: '',
        departmentId: '',
        departmentName: '',
        orgId: '',
      },
    }
  },
  getters: {
    getToken: (state: IOUser) => state.userInfo.openUserToken,
  },
  actions: {
    setUserInfo(data: IOUserInfo) {
      Object.assign(this.userInfo, data)
      localStorage.setItem('userInfo', JSON.stringify(data))
    },
  },
})
